
public function <?= $this->function_name ?>($dbh<?
foreach ($this->variables as $name => $setting) {
    if (!empty($setting['func_param'])) {
        echo ', $' . $name;
    }
}
?><? if ($this->pagination_function_enable): ?>,$pageNo,$pageSize<? endif ?>){

<?= $this->custom_header_code; ?>

$runQuery="no query yet";
try {
$selectColumns =" SELECT ";
<?
foreach ($this->columns as $ind => $one) {
    echo '$selectColumns .="';
    if ($ind !== 0) {
        echo ", ";
    }

    if (empty($one['table_alias'])) {
        if ($this->table_size === 1) {
            $columnName = "`{$one['column_name']}`";
        } else {
            $columnName = '{$this->' . $one['table_varialbe'] . '}.`' . $one['column_name'] . "`";
        }
    } else {
        $columnName = "`{$one['table_alias']}`.`{$one['column_name']}`";
    }

    $aliasName = $one['alias_name'];
    echo "{$columnName} `{$aliasName}`\";\n";
}
?>
$queryBase=" FROM ";
<?
foreach ($this->frome_tables as $one) {
    echo '$queryBase.="';
    if (!empty($one['join_type'])) {
        echo $one['join_type'] . " ";
    }
    echo '{' . $one['table_variable'] . '}';

    if (!empty($one['table_alias'])) {
        echo " as `{$one['table_alias']}` ";
    }
    echo "\";\n";
    if (!empty($one['logic_strings'])) {
        echo '$queryBase.=" on "';
        foreach ($one['logic_strings'] as $one) {
            echo "." . $one;
        }

        echo ";\n";
    }
}


if (!empty($this->where_claus)) {
    echo '$queryBase.=" ' . 'WHERE " ';
    foreach ($this->where_claus as $one) {
        echo "." . $one;
    }
    echo ";\n";
}
?>



$order_clause="";

<?
if (!empty($this->order_by)) {
    echo '$order_clause .=" ORDER BY ";' . "\n";
    foreach ($this->order_by as $ind => $one) {
        if ($ind !== 0) {
            echo '$order_clause.=", ";' . "\n";
        }

        if ($one['type'] === 'fixed') {
            if (empty($one['table_alias'])) {
                if ($this->table_size === 1) {
                    $columnName = "`{$one['column_name']}`";
                } else {
                    $columnName = '{$this->' . $one['table_varialbe'] . '}.' . "`{$one['column_name']}`";
                }
            } else {
                $columnName = "{$one['table_alias']}.`{$one['column_name']}`";
            }
            echo '$order_clause.="' . $columnName . "\";\n";

            if ($one['desc']) {
                echo '$order_clause.=" DESC";' . "\n";
            }
        } else {


            echo '$order_clause.="{$' . $one['variable'] . '}";' . "\n";
            echo '$order_clause.=$' . $one['desc_variable'] . '?" DESC":"";' . "\n";
        }
    }
}
?>

$limit_clause="";

<?
if (!empty($this->limit)) {
    $lstr = " LIMIT {$this->limit['from']} ,{$this->limit['num']}";
    echo '$limit_clause.="' . $lstr . "\";\n";
}
?>




<? if ($this->pagination_function_enable): ?>
    $runQuery="SELECT count(*) total ".$queryBase;
    $stmt = $dbh->prepare($runQuery);

    <?$this->generatPdoBinds('$stmt'); ?>

    $stmt_rv = $stmt->execute();

    if ($stmt_rv) {

    $total = intval($stmt->fetchColumn());

    $stmt->closeCursor();

    if ($pageSize <= 0) {
    $pageSize = 10;
    }

    if ($total === 0) {
    $totalPage = 0;
    } else {
    $totalPage = intval($total / $pageSize);

    if ($total % $pageSize !== 0) {
    $totalPage++;
    }
    }

    $re = array();
    $pageInfo=array('total' => $total, 'total_pages' => $totalPage,'page_size' => $pageSize);
    if($pageNo<1){
    $pageNo=1;
    }
    $pageInfo['page_no']=$pageNo;

    $pageNo--;
    if($pageNo*$pageSize>$total){
    $re['data']=array();
    }
    else{
    $from=$pageNo*$pageSize;
    $runQuery=$selectColumns. $queryBase.$order_clause." limit {$from},{$pageSize}";
    $stmt2= $dbh->prepare($runQuery);


     <?$this->generatPdoBinds('$stmt2'); ?>
    $stmt_rv2 = $stmt2->execute();
    if($stmt_rv2){
    $re['data']=$stmt2->fetchAll();
    $pageInfo['fetch_count']=$stmt2->rowCount();
    $re['page_info']=$pageInfo;
    }
    else{
    <? if (!empty($this->error_log)): ?>
        <?= $this->error_log ?>('db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery);
    <? else: ?>
        //$errorMessage='db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery;
    <? endif ?>
    $re['data']=false;
    }
    }
    return $re;
    }
    else {
    <? if (!empty($this->error_log)): ?>
        <?= $this->error_log ?>('db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery);
    <? else: ?>
        //$errorMessage='db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery;
    <? endif ?>
    return false;
    }
<? else: ?>

    $runQuery=$selectColumns . $queryBase . $order_clause . $limit_clause;

    //echo $runQuery."\n";

    $stmt = $dbh->prepare($runQuery);
     <?$this->generatPdoBinds('$stmt'); ?>
    $stmt_rv = $stmt->execute();

    if ($stmt_rv) {
    <? if ($this->query_return_first_row): ?>
        $re=$stmt->fetchAll();
        if(isset($re[0])){
        return $re[0];
        }
        else {
        return null;
        }
    <? else: ?>
        return $stmt->fetchAll();
    <? endif ?>
    }
    else {
    <? if (!empty($this->error_log)): ?>
        <?= $this->error_log ?>('db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery);
    <? else: ?>
        //$errorMessage='db error info:' . var_export($stmt->errorInfo(), true)." query:".$runQuery;
    <? endif ?>

    return false;
    }
<? endif ?>
}
catch ( PDOException $x ) {
<? if (!empty($this->error_log)): ?>
    <?= $this->error_log ?>('db error info:' . $x->getMessage()." query:".$runQuery);
<? else: ?>
    //$errorMessage='db error info:' . $x->getMessage()." query:".$runQuery;
<? endif ?>

throw $x;
}
<?= $this->custom_tailer_code; ?>
}
